<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:12
 */

namespace App\FreeBird\Models;

use App\Base\Models\AreaModel;
use App\Base\Models\BaseModel;
use App\FreeBird\Contracts\InfoClassInterface;
use Illuminate\Database\Eloquent\SoftDeletes;

class StoreModel extends BaseModel
{
    use SoftDeletes;
    /**
     * 订单信息表名
     * @var string
     */
    public $table = "freebird_store";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    protected $appends = ["country_name", "city_name","region_name"];
    /**
     * 地址模型
     * @return AreaModel
     */
    public function getAreaModel(){
        return app()->make(AreaModel::class);
    }
    /**
     * 省份 名称
     * @return string
     */
    public function getCountryNameAttribute(){
        $area_id=$this->country;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }
    /**
     * 城市 名称
     * @return string
     */
    public function getCityNameAttribute(){
        $area_id=$this->city;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }

    /**
     * 区 名称
     * @return string
     */
    public function getRegionNameAttribute(){
        $area_id=$this->region;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }


    public $rarr=array();
    /**
     * 得到父级数组
     * @param int
     * @return array
     */
    public function get_parent($myid,$refer,$pidname='pid')
    {
        if (!isset($refer[$myid])){
            return false;
        }
        if($refer[$myid][$pidname]==0){
            return true;
        }else{
            $this->rarr[]=$refer[$refer[$myid][$pidname]];  //父类值
            $this->get_parent($refer[$myid][$pidname],$refer);
        }
    }

    /**
     * 获取行业分类
     * @return InfoClassInterface
     */
    public function getInfoClassInterface(){
        return app()->make(InfoClassInterface::class);
    }

    public function setClassAttribute($value){
        //获取分类
        $classList=$this->getInfoClassInterface()->getClassAll([]);
        $classArr=$classList->toArray();

        $newArr=array();
        foreach ($classArr as $key=>$val){
            $newArr[$val['id']]=$val;
        }
        $this->rarr[]=$newArr[$value];
        $this->get_parent($value,$newArr);
       if(!empty($this->rarr)){
           $makeArr=array();

           foreach ($this->rarr as $sv)
           {
               $makeArr[]=$sv['name'];
           }
           $this->attributes['class_name'] = "|".implode("|",$makeArr)."|";
       }
        $this->attributes['class'] = $value;

    }



}